<?php
error_reporting(E_ALL ^ E_STRICT);
set_include_path('lib/'.PATH_SEPARATOR.'common/'.PATH_SEPARATOR.'');

require_once('local_globals.php');
@include_once('app_globals.php');
    
function __autoload($class_name) {
    require_once($class_name.'.php');
}

class bg_mysqli extends mysqli {
    public function __construct($host, $user, $pass, $db_name = null) {
        if ($db_name === null) parent::__construct($host, $user, $pass);
        else parent::__construct($host, $user, $pass, $db_name);

        if (mysqli_connect_error()) {
            throw new Exception('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

function db_connect($db = null, $cred = null) {
    global $DB_CREDENTIALS,$DEFAULT_DB;
    if ($cred === null) $cred = $DB_CREDENTIALS;
    assert('is_array($cred);');
    //if ($db->connect_errno) throw new Exception('Connect failed: '.$db->connect_error);
    if ($db === null) $db = $DEFAULT_DB;
    return new bg_mysqli($cred['host'],$cred['user'],$cred['pw'],$db);
}

?>